﻿// COPYRIGHT (C) Markeluo ALL RIGHTS RESERVED. 
// GITEE: https://gitee.com/luowanli/AntdUI 
// QQ: 563944808

using Furion;
using IMES.BLL;
using IMES.Entity;
using IMES.Utils;
using IMES.VO;
using Mapster;

namespace IMES.Page.SysRoleUser
{
    public partial class frm_UserEditor : AntdUI.Window, IFormLoader
    {
        private SysUserVO? _model;
        UserRep rep = App.GetService<UserRep>();
        private bool isAdd = true;
        public frm_UserEditor()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 编辑对象
        /// </summary>
        public SysUserVO? EditModel
        {
            get { return _model; }
        }

        public bool InitData(object[] args)
        {
            if (args != null && args.Length > 0 && args[0] is SysUserVO)
            {
                _model = (SysUserVO)args[0];
            }

            BindControl();
            return true;
        }

        private void BindControl()
        {
            if (_model == null) return;
            txtUserCode.Text = _model.UserCode;
            txtUserName.Text = _model.UserName;
            txtPwd.Text = _model.Password;
            txtPhone.Text = _model.Mobile;
            txtRemark.Text = _model.Remark;
            switchUsable.Checked = (bool)_model.Usable!;
            pgHeader.Text = "用户编辑";
            isAdd = false;
        }

        private bool ValidateData()
        {
            bool bolNext = true;
            if (_model == null) _model = new SysUserVO();
            _model.UserCode = txtUserCode.Text;
            _model.UserName = txtUserName.Text;
            _model.Password=txtPwd.Text;
            _model.Remark = txtRemark.Text;
            _model.Mobile = txtPhone.Text;
            _model.Usable = switchUsable.Checked;
            if (string.IsNullOrEmpty(_model.UserCode))
            {
                showWarning(AntdUI.TType.Warn, "账号不能为空!");
                bolNext = false;
            }
            if (string.IsNullOrEmpty(_model.UserName))
            {
                showWarning(AntdUI.TType.Warn, "用户姓名不能为空!");
                bolNext = false;
            }
            if (string.IsNullOrEmpty(_model.Password))
            {
                showWarning(AntdUI.TType.Warn, "密码不能为空!");
                bolNext = false;
            }
            return bolNext;
        }
        private void showWarning(AntdUI.TType type, string txt)
        {
            tipTxt.Icon = type;
            tipTxt.Text = txt;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //验证数据
            bool isNext = ValidateData();
            if (!isNext) return;
            if (_model!.Id > 0)
            {
                //编辑
                rep.Update(_model.Adapt<User>());
            }
            else
            {
                //新增
                var temp = new User() { UserCode = _model.UserCode, UserName = _model.UserName,Mobile=_model.Mobile,Password=_model.Password, Usable = _model.Usable, Remark = _model.Remark };
                rep.Insert(temp);
                _model = temp.Adapt<SysUserVO>();
            }
            this.DialogResult = DialogResult.OK;
        }
    }
}
